package com.tansun.easycare.rule.datasource.web;

import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.tansun.easycare.core.utils.ResponseTable;
import com.tansun.easycare.rule.common.JsonMessage;
import com.tansun.easycare.rule.common.ResMessage;
import com.tansun.easycare.rule.common.RuleBaseController;
import com.tansun.easycare.rule.common.SelBean;
import com.tansun.easycare.rule.common.model.ResponseVO;
import com.tansun.easycare.rule.datasource.service.RuleDatasourceService;
import com.tansun.easycare.rule.front.entity.RuleDatasource;

@RestController
@RequestMapping(value = "/ruleProj/datasource")
public class DatasourceController extends RuleBaseController{

	@Autowired
	private RuleDatasourceService ruleDatasourceService;

	@RequestMapping(value = {"index", ""})
	public ResponseVO list(@RequestBody RuleDatasource param) {
		PageInfo<RuleDatasource> page = this.ruleDatasourceService.findDataSource(param);
        ResponseTable<RuleDatasource>  resVo  = new  ResponseTable<RuleDatasource>(page);  
        return resVo;
	}
	
	
	
	@RequestMapping(value = "saveOrUpdate")
	public ResponseVO saveOrUpdate(@RequestBody RuleDatasource ruleDatasource) {
		ResponseVO ret = new ResponseVO();
		try {
			ruleDatasourceService.save(ruleDatasource);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			ret.setStatus(ResponseVO.ERROR);
			ret.setDescription(e.getMessage());
		}
		return ret;
		
	}
	@RequestMapping("getDataSource")
    public ResponseVO getDataSource(@RequestBody RuleDatasource ruleDatasource) throws Exception {
		ResponseVO ret = new ResponseVO();
        try {
        	RuleDatasource data = ruleDatasourceService.get(ruleDatasource);
        	ret.setData(data);
        } catch (Exception e) {
        	logger.error(e.getMessage(),e);
        	ret.setStatus(ResponseVO.ERROR);
			ret.setDescription(e.getMessage());
        }
        return ret;
    }
	@RequestMapping("deleteDataSource")
    public ResponseVO deleteDataSource(@RequestBody RuleDatasource ruleDatasource) throws Exception {
		ResponseVO ret = new ResponseVO();
        try {
        	ruleDatasourceService.delete(ruleDatasource);
        } catch (Exception e) {
        	logger.error(e.getMessage(),e);
        	ret.setStatus(ResponseVO.ERROR);
			ret.setDescription(e.getMessage());
        }
        return ret;
    }
	
	@RequestMapping("getDatasource")
	@ResponseBody
	public List<SelBean> getType() throws Exception {
		List<SelBean> result = new ArrayList<SelBean>();	
		List<RuleDatasource> list = ruleDatasourceService.findList(new RuleDatasource());
		for (RuleDatasource e : list){
			result.add(new SelBean(e.getDsName(),e.getId()));
		}
		return result;
	}
    /**
     * 连接测试
     * @param ruleDatasource
     * @return
     */
    @RequestMapping("/connectionTest")
    @ResponseBody
    public ResponseVO connectionTest(@RequestBody RuleDatasource ruleDatasource) {
    	ResponseVO responseVo = new ResponseVO();
        try {
        	DriverManager.getConnection(ruleDatasource.getDsUrl(), ruleDatasource.getDsUsername(), ruleDatasource.getDsPwd());
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            responseVo.setStatus(ResponseVO.ERROR);
            responseVo.setDescription(e.getMessage());
        }
        return responseVo;
    }
	/**
	 * 从数据源导入枚举
	 * @param param
	 * @return
	 */
	@RequestMapping(value = "saveImport")
	@ResponseBody
	public ResMessage saveImport(@RequestParam Map<String,String> param) {
		try {
			ruleDatasourceService.saveEnum(param);
		} catch (Exception e) {
			log.error("导入枚举失败", e);
			return new ResMessage("failure", e.getMessage());
		}
		return new ResMessage("success", "保存成功");
	}
}
